<template>
  <!-- 分类商品 -->
  <div class="ref-goods" v-for="item in list" :key="item.id">
    <div class="head">
      <h3>- {{ item.name }} -</h3>
      <p class="tag">温暖柔软，品质之选</p>
      <XtxMore />
    </div>
    <div class="body">
      <RouterLink
        v-for="it in item.goods"
        to="/"
        :key="it.id"
        class="goods-item"
      >
        <img :src="it.picture" alt="" />
        <p class="name ellipsis">{{ it.name }}</p>
        <p class="desc ellipsis">{{ it.desc }}</p>
        <p class="price">&yen;{{ it.price }}</p>
      </RouterLink>
    </div>
  </div>
</template>

<script>
// 导入 api
import { findTopCategory } from '@/api/category.js'
// 导入 route
import { useRoute } from 'vue-router'
// 导入 ref
import { ref, watch } from 'vue'
export default {
  name: 'CategoryGoodsItems',
  setup () {
    const route = useRoute()
    const list = ref([])
    // findTopCategory(route.params.id).then(res => {
    //   list.value = res.result.children
    // })

    // 检测路由的改变
    // 参数1:观察的数据
    // 参数2:执行的回调函数
    // 参数3: 选项
    watch(
      route,
      () => {
        // console.log(route.path)
        // 屏蔽掉  一级分类 切换到 二级分类的情况
        if (route.path === `/category/${route.params.id}`) {
          // console.log('改变啦!')
          findTopCategory(route.params.id).then(res => {
            list.value = res.result.children
          })
        }
      },
      {
        // 立刻执行
        immediate: true
      }
    )
    // 返回
    return {
      list
    }
  }
}
</script>

<style scoped lang="less">
.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
  }
  .goods-item {
    display: block;
    width: 220px;
    padding: 20px 30px;
    text-align: center;
    .hoverShadow();
    img {
      width: 160px;
      height: 160px;
    }
    p {
      padding-top: 10px;
    }
    .name {
      font-size: 16px;
    }
    .desc {
      color: #999;
      height: 29px;
    }
    .price {
      color: @priceColor;
      font-size: 20px;
    }
  }
}
</style>
